#################################################
### Replication Code for Figure S8            ###
### Title: Mimicking Democracy                ###
### Authors: Hsu Yumin Wang, Eddy S. F. Yeung ###
### Version: April 25, 2024                   ###
#################################################

rm(list = ls())
setwd("~/Desktop/redist-propaganda/replication/Study 2")

## CUGS ----
data.cugs <- read.csv("CUGS Final Data.csv", stringsAsFactors = FALSE)
data.cugs$SITECITY[which(data.cugs$SITECITY == "毫州")] <- "亳州"

data.cugs$year <- 2018
data.cugs$date <- as.Date(paste0(data.cugs$year, "-", data.cugs$INTMON, "-", data.cugs$INTDAT))

data.cugs <- as.data.frame(data.cugs) # convert it to the most basic data format
data.cugs <- data.cugs[is.na(data.cugs$date) == FALSE, ]

data.cugs$tipi1.n[data.cugs$XG16A1=="非常同意"]<-5
data.cugs$tipi1.n[data.cugs$XG16A1=="比较同意"]<-4
data.cugs$tipi1.n[data.cugs$XG16A1=="一般"]<-3
data.cugs$tipi1.n[data.cugs$XG16A1=="不太同意"]<-2
data.cugs$tipi1.n[data.cugs$XG16A1=="非常不同意"]<-1

data.cugs$tipi2.n[data.cugs$XG16B1=="非常同意"]<-1
data.cugs$tipi2.n[data.cugs$XG16B1=="比较同意"]<-2
data.cugs$tipi2.n[data.cugs$XG16B1=="一般"]<-3
data.cugs$tipi2.n[data.cugs$XG16B1=="不太同意"]<-4
data.cugs$tipi2.n[data.cugs$XG16B1=="非常不同意"]<-5

data.cugs$tipi3.n[data.cugs$XG16J1=="非常同意"]<-5
data.cugs$tipi3.n[data.cugs$XG16J1=="比较同意"]<-4
data.cugs$tipi3.n[data.cugs$XG16J1=="一般"]<-3
data.cugs$tipi3.n[data.cugs$XG16J1=="不太同意"]<-2
data.cugs$tipi3.n[data.cugs$XG16J1=="非常不同意"]<-1

data.cugs$tipi4.n[data.cugs$XG16C1=="非常同意"]<-1
data.cugs$tipi4.n[data.cugs$XG16C1=="比较同意"]<-2
data.cugs$tipi4.n[data.cugs$XG16C1=="一般"]<-3
data.cugs$tipi4.n[data.cugs$XG16C1=="不太同意"]<-4
data.cugs$tipi4.n[data.cugs$XG16C1=="非常不同意"]<-5

data.cugs$tipi5.n[data.cugs$XG16D1=="非常同意"]<-5
data.cugs$tipi5.n[data.cugs$XG16D1=="比较同意"]<-4
data.cugs$tipi5.n[data.cugs$XG16D1=="一般"]<-3
data.cugs$tipi5.n[data.cugs$XG16D1=="不太同意"]<-2
data.cugs$tipi5.n[data.cugs$XG16D1=="非常不同意"]<-1

data.cugs$tipi6.n[data.cugs$XG16E1=="非常同意"]<-1
data.cugs$tipi6.n[data.cugs$XG16E1=="比较同意"]<-2
data.cugs$tipi6.n[data.cugs$XG16E1=="一般"]<-3
data.cugs$tipi6.n[data.cugs$XG16E1=="不太同意"]<-4
data.cugs$tipi6.n[data.cugs$XG16E1=="非常不同意"]<-5

data.cugs$tipi7.n[data.cugs$XG16F1=="非常同意"]<-5
data.cugs$tipi7.n[data.cugs$XG16F1=="比较同意"]<-4
data.cugs$tipi7.n[data.cugs$XG16F1=="一般"]<-3
data.cugs$tipi7.n[data.cugs$XG16F1=="不太同意"]<-2
data.cugs$tipi7.n[data.cugs$XG16F1=="非常不同意"]<-1

data.cugs$tipi8.n[data.cugs$XG16G1=="非常同意"]<-1
data.cugs$tipi8.n[data.cugs$XG16G1=="比较同意"]<-2
data.cugs$tipi8.n[data.cugs$XG16G1=="一般"]<-3
data.cugs$tipi8.n[data.cugs$XG16G1=="不太同意"]<-4
data.cugs$tipi8.n[data.cugs$XG16G1=="非常不同意"]<-5

data.cugs$tipi9.n[data.cugs$XG16H1=="非常同意"]<-5
data.cugs$tipi9.n[data.cugs$XG16H1=="比较同意"]<-4
data.cugs$tipi9.n[data.cugs$XG16H1=="一般"]<-3
data.cugs$tipi9.n[data.cugs$XG16H1=="不太同意"]<-2
data.cugs$tipi9.n[data.cugs$XG16H1=="非常不同意"]<-1

data.cugs$tipi10.n[data.cugs$XG16I1=="非常同意"]<-1
data.cugs$tipi10.n[data.cugs$XG16I1=="比较同意"]<-2
data.cugs$tipi10.n[data.cugs$XG16I1=="一般"]<-3
data.cugs$tipi10.n[data.cugs$XG16I1=="不太同意"]<-4
data.cugs$tipi10.n[data.cugs$XG16I1=="非常不同意"]<-5

data.cugs$tipi.pers.x <- (data.cugs$tipi1.n+data.cugs$tipi6.n)/2  
data.cugs$tipi.pers.a <- (data.cugs$tipi2.n+data.cugs$tipi7.n)/2
data.cugs$tipi.pers.c <- (data.cugs$tipi3.n+data.cugs$tipi8.n)/2
data.cugs$tipi.pers.e <- (data.cugs$tipi4.n+data.cugs$tipi9.n)/2
data.cugs$tipi.pers.o <- (data.cugs$tipi5.n+data.cugs$tipi10.n)/2

## Our data ----
df <- read.csv("main_data.csv")

# Personality: agreeableness
df$agreeableness <- ((6 - df$TIPI_2) + df$TIPI_7) / 2

# Personality: conscientiousness
df$conscientiousness <- (df$TIPI_3 + (6 - df$TIPI_8)) / 2

# Personality: extraversion
df$extraversion <- (df$TIPI_1 + (6 - df$TIPI_6)) / 2

## Comparison ----
# Personality: agreeableness
data.cugs$agreeableness <- ((6 - data.cugs$tipi2.n) + data.cugs$tipi7.n) / 2
prop.table(table(data.cugs$agreeableness))
prop.table(table(df$agreeableness))
table(data.cugs$agreeableness)
table(df$agreeableness)
hist(data.cugs$agreeableness)
hist(df$agreeableness)
mean(data.cugs$agreeableness, na.rm=TRUE)
mean(df$agreeableness, na.rm=TRUE)

# Personality: conscientiousness
data.cugs$conscientiousness <- (data.cugs$tipi3.n + (6 - data.cugs$tipi8.n)) / 2
prop.table(table(data.cugs$conscientiousness))
prop.table(table(df$conscientiousness))
table(data.cugs$conscientiousness)
table(df$conscientiousness)
hist(data.cugs$conscientiousness)
hist(df$conscientiousness)
mean(data.cugs$conscientiousness, na.rm=TRUE)
mean(df$conscientiousness, na.rm=TRUE)

# Personality: extraversion
data.cugs$extraversion <- (data.cugs$tipi1.n + (6 - data.cugs$tipi6.n)) / 2
prop.table(table(data.cugs$extraversion))
prop.table(table(df$extraversion))
table(data.cugs$extraversion)
table(df$extraversion)
hist(data.cugs$extraversion)
hist(df$extraversion)
mean(data.cugs$extraversion, na.rm=TRUE)
mean(df$extraversion, na.rm=TRUE)

personality<-cbind(agreeableness_CUSG
=prop.table(table(data.cugs$agreeableness)),
agreeableness_ours
=prop.table(table(df$agreeableness)),
conscientiousness_CUGS
=prop.table(table(data.cugs$conscientiousness)),
conscientiousness_ours
=prop.table(table(df$conscientiousness)),
extraversion_CUGS
=prop.table(table(data.cugs$extraversion)),
extraversion_ours
=prop.table(table(df$extraversion)))

agreeableness<-data.frame(scale=(seq(1,5,by=0.5)),
  share=c(prop.table(table(data.cugs$agreeableness)),prop.table(table(df$agreeableness))),
  data=c(rep("China Urban Governance Survey  ",9),rep("Our Sample",9)))

conscientiousness<-data.frame(scale=(seq(1,5,by=0.5)),
                              share=c(prop.table(table(data.cugs$conscientiousness)),prop.table(table(df$conscientiousness))),
                              data=c(rep("China Urban Governance Survey  ",9),rep("Our Sample",9)))

extraversion<-data.frame(scale=(seq(1,5,by=0.5)),
                          share=c(prop.table(table(data.cugs$extraversion)),prop.table(table(df$extraversion))),
                          data=c(rep("China Urban Governance Survey  ",9),rep("Our Sample",9)))


agreeableness_plot<-ggplot(agreeableness, aes(x=scale, y=share, fill=data)) +
  geom_bar(stat="identity", position=position_dodge(), color="black")+
  scale_fill_grey()+
  theme_minimal()+
  xlab("Agreeableness")+
  ylab("")+
  coord_cartesian(ylim=c(0,0.5))+
  theme(legend.position = "top",
        legend.title=element_blank(),
        text = element_text(family = "Times", size = 12),
        axis.text = element_text(family = "Times", size = 11))

conscientiousness_plot<-ggplot(conscientiousness, aes(x=scale, y=share, fill=data)) +
  geom_bar(stat="identity", position=position_dodge(), color="black")+
  scale_fill_grey()+
  theme_minimal()+
  xlab("Conscientiousness")+
  ylab("Proportion")+
  coord_cartesian(ylim=c(0,0.5))+
  theme(legend.position = "top",
        legend.title=element_blank(),
        text = element_text(family = "Times", size = 12),
        axis.text = element_text(family = "Times", size = 11))

extraversion_plot<-ggplot(extraversion, aes(x=scale, y=share, fill=data)) +
  geom_bar(stat="identity", position=position_dodge(), color="black")+
  scale_fill_grey()+
  theme_minimal()+
  xlab("Extraversion")+
  ylab("")+
  coord_cartesian(ylim=c(0,0.5))+
  theme(legend.position = "top",
        legend.title=element_blank(),
        text = element_text(family = "Times", size = 12),
        axis.text = element_text(family = "Times", size = 11))

nt <- theme(legend.position='none')
personality_comparison<-grid_arrange_shared_legend(agreeableness_plot, arrangeGrob(conscientiousness_plot+nt, extraversion_plot+nt, ncol=2), ncol=1, nrow=2)

ggsave("personality_comparison.pdf",personality_comparison, width=10, height=8) 
